﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HRMS_Model;
using System.Diagnostics;

namespace HRMS.Models
{
    public class DegreesModels
    {
        public static bool AddDegree(Degree degree)
        {
            try
            {
            using (var degEntity = new HRMSEntities())
            {
                degEntity.Degrees.AddObject(degree);
                degEntity.SaveChanges();
                return true;
            }
        }

        catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static bool DeleteDegree(int id)
        {
            try
            {
                using (var degEntity = new HRMSEntities())
                {
                    degEntity.Degrees.DeleteObject(degEntity.Degrees.First(d => d.DegreeId == id));
                    degEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static List<Degree> GetDegrees(string sortExpression, string sortDirection, int pageIndex, int pageSize, int pageCount)
        {
            using (var degEntity = new HRMSEntities())
            {
                return degEntity.Degrees.OrderBy("it." + sortExpression + " " + sortDirection).Skip(pageIndex * pageSize).Take(pageCount * pageSize).ToList();
            }
        }

        public static int GetDegreesCount()
        {
            using (var degEntity = new HRMSEntities())
            {
                return degEntity.Degrees.Count();
            }
        }

        public static bool UpdateDegree(Degree degree)
        {
            try
            {
                using (var degEntity = new HRMSEntities())
                {
                    var degreeToEdit = degEntity.Degrees.FirstOrDefault(d => d.DegreeId == degree.DegreeId);
                    if (degreeToEdit != null) degreeToEdit.DegreeName = degree.DegreeName;
                    degEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static List<string> DegreesList()
        {
            var degreesList = new List<string>();
            try
            {
                using (var degreeEntity = new HRMSEntities())
                {
                    var degrees = degreeEntity.Degrees.ToList();
                    degreesList.AddRange(degrees.Select(degree => degree.DegreeName));
                }
                return degreesList;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return degreesList;
            }
        }         
    }
}